package com.zycfc.zsf.boot.core.builder;

import org.yaml.snakeyaml.*;
import java.util.*;
import java.io.*;
import org.slf4j.*;

public class YamlReader
{
    private static final Logger log;
    private static Map<String, Map<String, Object>> properties;
    public static final YamlReader instance;
    
    public Object getValueByKey(final String fileName, final String key) {
        final Yaml yaml = new Yaml();
        try (final InputStream in = YamlReader.class.getClassLoader().getResourceAsStream(fileName)) {
            YamlReader.properties = (Map<String, Map<String, Object>>)yaml.loadAs(in, (Class)HashMap.class);
        }
        catch (Exception e) {
            YamlReader.log.error("Init yaml failed !", (Throwable)e);
        }
        final String separator = ".";
        String[] separatorKeys = null;
        if (key.contains(separator)) {
            separatorKeys = key.split("\\.");
            Map<String, Map<String, Object>> finalValue = new HashMap<String, Map<String, Object>>();
            for (int i = 0; i < separatorKeys.length - 1; ++i) {
                if (i == 0) {
                    finalValue = (Map<String, Map<String, Object>>)YamlReader.properties.get(separatorKeys[i]);
                }
                else {
                    if (finalValue == null) {
                        break;
                    }
                    finalValue = (Map<String, Map<String, Object>>)finalValue.get(separatorKeys[i]);
                }
            }
            return (finalValue == null) ? null : finalValue.get(separatorKeys[separatorKeys.length - 1]);
        }
        return YamlReader.properties.get(key);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)YamlReader.class);
        YamlReader.properties = new HashMap<String, Map<String, Object>>();
        instance = new YamlReader();
    }
}
